<?php
namespace app\manage\controller;

use think\facade\Db;
use app\manage\controller\Base;
use app\model\OrderModel;
use app\model\UserModel;
use support\Request;
use support\View;

class OrderController extends Base
{

    public function index(Request $request)
    {
        $order=new OrderModel(); 
      	if(request()->method()=='POST') {
            $data=$request->post();
            
            $start=strtotime($data['start']);
            $end=strtotime($data['end']);
            if (isset($data['start']) or isset($data['end'])) {
                $map=[];
                if ($data['start'] != '') {
                    $map[]=	['o.create_time','>',strtotime($data['start'])];
                }
                if ($data['end'] != '') {
                    $map[]=	['o.create_time','<',strtotime($data['end'])+60*60*24];
                }
            } else {
                $map=true;
            }
            
            if ($data['type']=='so') {
                $order=$order
                ->alias('o')
                ->join('user m', "o.user_id = m.id", 'LEFT')
                ->join('goods g', "o.goods_id = g.id", 'LEFT')
                ->field('o.*,m.name,m.phone,m.nickName as username,m.phone as mphone,g.goods_name')
                ->where($map)
                ->where(function ($query) use ($data) {
                    if ($data['key']) {
                        $query->whereOr('m.phone', 'like', '%'.$data['key'].'%')
                            ->whereOr('m.nickName', 'like', '%'.$data['key'].'%')
                            ->whereOr('out_trade_no', 'like', '%'.$data['key'].'%')
                            ->whereOr('m.name', 'like', '%'.$data['key'].'%');
                    }
                })
                ->order('create_time desc,id desc')->paginate(['list_rows'=>12,'query'=>request()->all()]);
            }
        } else {
            $order=$order
            ->alias('o')
            ->join('user m', "o.user_id = m.id", 'LEFT')
            ->join('goods g', "o.goods_id = g.id", 'LEFT')
            ->field('o.*,m.name,m.name as username,m.phone as mphone,g.goods_name')
            ->order('create_time desc,id desc')->paginate(['list_rows'=>12,'query'=>request()->all()]);
        }
        
        View::assign('start', $data['start'] ?? '');
        View::assign('end', $data['end'] ?? '');
        View::assign('key', $data['key'] ?? '');
        
        View::assign('order', $order);
        View::assign('count1', $order->total());
        
        
        return view('order/index');
    }
   
    public function ajax(Request $request)
    {
        $data=$request->all();
        $order=new Ordermodel();
        if ($data['type']=='order_del') {
            $id=$data['id'];
            $info=$order->destroy($id);
            if ($info) {
                return json(['code'=>1]);
            } else {
                return json(['code'=>0]);
            }
        }
        if ($data['type']=='order_tui') {
            $id=$data['id'];
            $order=Db::name('order')->find($id);
            if ($order["pay_status"]==0) {
                return json(['code'=>0,'message'=>'支付后才可以退还！']);
            }
            if ($order["is_tui"]==1) {
                return json(['code'=>0,'message'=>'已经退还不能重复！']);
            }
            if ($order["payment"]!=3) {
                return json(['code'=>0,'message'=>'目前只允许退还，alg支付的！']);
            }
            $alg=Db::name('alg')->where('order_id', $order['id'])->where('type', 8)->where('uid', $order['user_id'])->find();
            Db::name('alg')->insert([
                'time'=>time(),
                'type'=>11,
                'num'=>$alg['num'],
                'uid'=>$order['user_id'],
                'order_id'=>$order['id']
            ]);
            Db::name('user')->where('id', $order['user_id'])->setInc('alg', $alg['num']);
            Db::name('order')->where('id', $order['id'])->update(['is_tui'=>1]);
            return json(['code'=>1,'message'=>'成功']);
        }
        if ($data['type']=='get_shi') {
            $id=$data['id'];
            $info=Db::name('area')->where('fid', $id)->select();
            if ($info) {
                return json([
                    'code'=>1,
                    'data'=>$info
                ]);
            } else {
                return json([
                    'code'=>0,
                    'data'=>$info
                ]);
            }
        }
        return json(['code'=>0]);
    }

    public function edit(Request $request)
    {  
     	$order = new Ordermodel();   

         $id=$request->get('id',0);
    	if(request()->method()=='POST') {
            $data=$request->post();
                
            if (isset($data['pay_status']) and $data['pay_status']) {
                //分钱
                $order=Db::name('order')->find($id);
                $fen=Db::name('order')->where('id', $order['id'])->where('pay_status', 0)->update([
                    'pay_status'=>1,
                    'pay_price'=>$order['goods_price_zong'],//支付的金额
                    'order_time'=>time(),
                ]);
            }
        

            $res=$order->save($data, ['id' =>$id]);
            $ding=$order->where('order_status', '0')->count();
            if ($res) {
                if ($ding!=0) {
                    echo '<script>parent.parent.document.getElementById("lizhili_ding").innerHTML="订',$ding,'"</script>';
                } else {
                    echo '<script>parent.parent.document.getElementById("lizhili_ding").innerHTML="订"</script>';
                }
            
                return view_s('修改成功', url('order/index', ['st'=>1]));
            } else {
                return view_e('订单修改失败了');
            }
        }
        $data= $order
                ->alias('o')
                ->join('user m', "o.user_id = m.id", 'LEFT')
                ->field('o.*,m.name,m.phone,m.nickName as username,m.phone as mphone,m.address')
                ->where('o.id', $id)
                ->find();
        View::assign('data', $data); 
        return view('order/edit');
    }
    
    protected function fen($uid=0, $money=0)
    {
        if ($uid or $money) {
            //找到所有上一级
            $arr=$this->shang($uid);
            $a=[];
            foreach ($arr as $k=>$v) {
                if ($k==0) {
                    $a[]=[
                        'id'=>$v,
                        'money'=>$money*0.12
                    ];
                } else {
                    $a[]=[
                        'id'=>$v,
                        'money'=>$money*0.12*(0.5**$k)
                    ];
                }
            }
            foreach ($a as $v) {
                Db::name('user')->where('id', $v['id'])->setInc('alg_zong', $v['money']);
            }
        }
    }
    protected function shang($uid=0)
    {
        $m=Db::name('user')->where('id', $uid)->value('fid');
        static $arr_li=[];
        if ($m) {
            $arr_li[]=$m;
            $this->shang($m);
        }
        return $arr_li;
    }
}
